﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace YJ.Data.ORACLE
{
    public class ProgramBuilderQuerys : YJ.Data.Interface.IProgramBuilderQuerys
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public ProgramBuilderQuerys()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderQuerys实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.ProgramBuilderQuerys model)
        {
            string sql = @"INSERT INTO ProgramBuilderQuerys
				(ID,ProgramID,Field,ShowTitle,Operators,ControlName,InputType,Width,Sort,DataSource,DataSourceString,DataLinkID,IsQueryUsers,Value) 
				VALUES(:ID,:ProgramID,:Field,:ShowTitle,:Operators,:ControlName,:InputType,:Width,:Sort,:DataSource,:DataSourceString,:DataLinkID,:IsQueryUsers,:Value)";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID },
				new OracleParameter(":ProgramID", OracleDbType.Varchar2){ Value = model.ProgramID },
				new OracleParameter(":Field", OracleDbType.Varchar2){ Value = model.Field },
				new OracleParameter(":ShowTitle", OracleDbType.Varchar2){ Value = model.ShowTitle },
				new OracleParameter(":Operators", OracleDbType.Varchar2){ Value = model.Operators },
				new OracleParameter(":ControlName", OracleDbType.Varchar2){ Value = model.ControlName },
				new OracleParameter(":InputType", OracleDbType.Int32){ Value = model.InputType },
				model.Width == null ? new OracleParameter(":Width", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":Width", OracleDbType.Varchar2) { Value = model.Width },
				new OracleParameter(":Sort", OracleDbType.Int32){ Value = model.Sort },
				model.DataSource == null ? new OracleParameter(":DataSource", OracleDbType.Int32) { Value = DBNull.Value } : new OracleParameter(":DataSource", OracleDbType.Int32) { Value = model.DataSource },
				model.DataSourceString == null ? new OracleParameter(":DataSourceString", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":DataSourceString", OracleDbType.Varchar2) { Value = model.DataSourceString },
				model.DataLinkID == null ? new OracleParameter(":DataLinkID", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":DataLinkID", OracleDbType.Varchar2) { Value = model.DataLinkID },
				model.IsQueryUsers == null ? new OracleParameter(":IsQueryUsers", OracleDbType.Int32) { Value = DBNull.Value } : new OracleParameter(":IsQueryUsers", OracleDbType.Int32) { Value = model.IsQueryUsers },
				model.Value == null ? new OracleParameter(":Value", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":Value", OracleDbType.Varchar2) { Value = model.Value }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderQuerys实体类</param>
        public int Update(YJ.Data.Model.ProgramBuilderQuerys model)
        {
            string sql = @"UPDATE ProgramBuilderQuerys SET 
				ProgramID=:ProgramID,Field=:Field,ShowTitle=:ShowTitle,Operators=:Operators,ControlName=:ControlName,InputType=:InputType,Width=:Width,Sort=:Sort,DataSource=:DataSource,DataSourceString=:DataSourceString,DataLinkID=:DataLinkID,IsQueryUsers=:IsQueryUsers,Value=:Value
				WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ProgramID", OracleDbType.Varchar2){ Value = model.ProgramID },
				new OracleParameter(":Field", OracleDbType.Varchar2){ Value = model.Field },
				new OracleParameter(":ShowTitle", OracleDbType.Varchar2){ Value = model.ShowTitle },
				new OracleParameter(":Operators", OracleDbType.Varchar2){ Value = model.Operators },
				new OracleParameter(":ControlName", OracleDbType.Varchar2){ Value = model.ControlName },
				new OracleParameter(":InputType", OracleDbType.Int32){ Value = model.InputType },
				model.Width == null ? new OracleParameter(":Width", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":Width", OracleDbType.Varchar2) { Value = model.Width },
				new OracleParameter(":Sort", OracleDbType.Int32, -1){ Value = model.Sort },
				model.DataSource == null ? new OracleParameter(":DataSource", OracleDbType.Int32) { Value = DBNull.Value } : new OracleParameter(":DataSource", OracleDbType.Int32) { Value = model.DataSource },
				model.DataSourceString == null ? new OracleParameter(":DataSourceString", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":DataSourceString", OracleDbType.Varchar2) { Value = model.DataSourceString },
				model.DataLinkID == null ? new OracleParameter(":DataLinkID", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":DataLinkID", OracleDbType.Varchar2) { Value = model.DataLinkID },
				model.IsQueryUsers == null ? new OracleParameter(":IsQueryUsers", OracleDbType.Int32) { Value = DBNull.Value } : new OracleParameter(":IsQueryUsers", OracleDbType.Int32) { Value = model.IsQueryUsers },
				model.Value == null ? new OracleParameter(":Value", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":Value", OracleDbType.Varchar2) { Value = model.Value },
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM ProgramBuilderQuerys WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.ProgramBuilderQuerys> DataReaderToList(OracleDataReader dataReader)
        {
            List<YJ.Data.Model.ProgramBuilderQuerys> List = new List<YJ.Data.Model.ProgramBuilderQuerys>();
            YJ.Data.Model.ProgramBuilderQuerys model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.ProgramBuilderQuerys();
                model.ID = dataReader.GetString(0).ToGuid();
                model.ProgramID = dataReader.GetString(1).ToGuid();
                model.Field = dataReader.GetString(2);
                model.ShowTitle = dataReader.GetString(3);
                model.Operators = dataReader.GetString(4);
                model.ControlName = dataReader.GetString(5);
                model.InputType = dataReader.GetInt32(6);
                if (!dataReader.IsDBNull(7))
                    model.Width = dataReader.GetString(7);
                model.Sort = dataReader.GetInt32(8);
                if (!dataReader.IsDBNull(9))
                    model.DataSource = dataReader.GetInt32(9);
                if (!dataReader.IsDBNull(10))
                    model.DataSourceString = dataReader.GetString(10);
                if (!dataReader.IsDBNull(11))
                    model.DataLinkID = dataReader.GetString(11);
                if (!dataReader.IsDBNull(12))
                    model.IsQueryUsers = dataReader.GetInt32(12);
                if (!dataReader.IsDBNull(13))
                    model.Value = dataReader.GetString(13);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderQuerys> GetAll()
        {
            string sql = "SELECT * FROM ProgramBuilderQuerys";
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.ProgramBuilderQuerys> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM ProgramBuilderQuerys";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.ProgramBuilderQuerys Get(Guid id)
        {
            string sql = "SELECT * FROM ProgramBuilderQuerys WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderQuerys> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderQuerys> GetAll(Guid programID)
        {
            string sql = "SELECT * FROM ProgramBuilderQuerys WHERE ProgramID=:ProgramID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ProgramID", OracleDbType.Varchar2){ Value = programID }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderQuerys> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByProgramID(Guid id)
        {
            string sql = "DELETE FROM ProgramBuilderQuerys WHERE ProgramID=:ProgramID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ProgramID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }

       
    }
}